package com.kantboot.system.admin.service.impl;

import com.kantboot.system.admin.domain.dto.BindPermissionDTO;
import com.kantboot.system.admin.domain.dto.SysAdminRouterSearchDTO;
import com.kantboot.system.admin.domain.entity.SysAdminRouter;
import com.kantboot.system.admin.domain.entity.SysAdminRouterPermission;
import com.kantboot.system.admin.repository.SysAdminRouterPermissionRepository;
import com.kantboot.system.admin.repository.SysAdminRouterRepository;
import com.kantboot.system.admin.service.ISysAdminRouterService;
import com.kantboot.system.user.service.ISysPermissionService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysAdminRouterServiceImpl implements ISysAdminRouterService {

    @Resource
    private SysAdminRouterRepository repository;

    @Resource
    private IUserSelfService userSelfService;

    @Resource
    private ISysPermissionService permissionService;


    @Override
    public PageResult getBodyData(PageParam<SysAdminRouterSearchDTO> pageParam) {
        return PageResult.of(repository.getBodyData(pageParam.getData(),pageParam.getPageable()));
    }

    @Override
    public List<SysAdminRouter> getList() {
        return repository.findAll();
    }

    /**
     * 根据用户id获取路由列表
     */
    @Override
    public List<SysAdminRouter> getListByUserId(Long userId) {
        List<String> codeListByUserId = permissionService.getCodeListByUserId(userId);
        return repository.findListByPermissionCodeList(codeListByUserId);
    }

    /**
     * 获取用户自身的路由列表
     */
    @Override
    public List<SysAdminRouter> getSelfList() {
        Long selfId = userSelfService.getSelfId();
        return getListByUserId(selfId);
    }

    @Override
    public List<SysAdminRouter> getSelfTopList() {
        Long selfId = userSelfService.getSelfId();
        List<String> codeListByUserId = permissionService.getCodeListByUserId(selfId);
        return repository.findTopListByPermissionCodeList(codeListByUserId);
    }

    @Resource
    private SysAdminRouterPermissionRepository routerPermissionRepository;

    @Override
    public void bindPermission(BindPermissionDTO dto) {
        // 删除原有的权限
        routerPermissionRepository.deleteByRouterCode(dto.getRouterCode());
        // 新增权限
        List<SysAdminRouterPermission> routerPermissionList = new ArrayList<>();
        for (String permissionCode : dto.getPermissionCodeList()) {
                routerPermissionList.add(new SysAdminRouterPermission()
                        .setRouterCode(dto.getRouterCode())
                        .setPermissionCode(permissionCode)
                );
        }
        routerPermissionRepository.saveAll(routerPermissionList);
    }

    @Override
    public List<String> getPermissionCodeListByRouterCode(String routerCode) {
        return routerPermissionRepository.findPermissionCodeListByRouterCode(routerCode);
    }
}
